package com.llmcu.msb.architect.concurrent.basic;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * interrupt() 无法打断 lockInterruptibly()
 *
 * @author liuling
 * @date 2024/9/17 19:44
 */
public class InterruptAndLockInterruptibly {

    static Object object = new Object();

    public static void main(String[] args) throws InterruptedException {
        ReentrantLock lock = new ReentrantLock();
        new Thread(() -> {
            try {
                lock.lockInterruptibly();
                System.out.println("t1 start...");
                try {
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("t1 end...");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }

        }).start();
        TimeUnit.SECONDS.sleep(1);
        Thread t2 = new Thread(() -> {
            System.out.println("t2 palying...");
            try {
                lock.lockInterruptibly();
                System.out.println("t2 start...");
                System.out.println("t2 end...");
            } catch (InterruptedException e) {
                System.out.println("t2被打断...");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        });
        t2.start();
        TimeUnit.SECONDS.sleep(1);
        t2.interrupt();
        System.out.println("主线程已执行t2打断操作，并执行完毕下班");

    }

}
